require(['highcharts','reportCommon'], function () {
	var carId,carName,dateType="02",enerType="oil",titleStr="",
	//所有的  ：oil-油、ele-电、gas-气、per-指标环比
	//总能耗  
	totalEnerOil="",totalEnerGas="",totalEnerEle="",totalEnerOilPer="",totalEnerGasPer="",totalEnerElePer="",
	//百公里能耗
	kmEnerOil="",kmEnerGas="",kmEnerEle="",kmEnerOilPer="",kmEnerGasPer="",kmEnerElePer="",
	//能耗排名
	enerRankOil="",enerRankEle="",enerRankGas="",enerRankOilPer="",enerRankElePer="",enerRankGasPer="",
	//百公里
	tmpDate=[],carOilData=[],lineOilData=[],carEleData=[],lineEleData=[],carGasData=[],lineGasData=[],carMileData=[],lineAvgData=[],
	//饼图
	energyPctOil=[],energyPctEle=[],energyPctGas=[],
	//排名
	trHtml,trHtmlOil,trHtmlEle,trHtmlGas;

    function carChange(event, treeId, treeNode) {
        if (treeNode.type === 'CAR') {
            carId = treeNode.id;
            carName = treeNode.name;
            clear();
            $(".tab-panel3 a")[0].click();
            //标题时间
            titleStr = chartTitle(dateType);
            //燃料类型、总能耗、百公里能耗、能耗排名
            getCarAnaEnerIdx(carId, dateType);
            //车辆百公里能耗趋势     车辆里程趋势
            getCarAnaEnerTrend(carId, dateType);
            //能耗排名
            getCarAnaEnerRank(carId, dateType);
            //能耗转速分析
            getCarAnaEnergySpdAna(carId, dateType);
        } else {
            $.messager.alert("提示", "请选择一辆车进行查看。");
        }
    }
	//左侧树
	commonFun.leftTreeNodeClickObj['carAnaEnergyInfo'] = carChange;

	//清空历史记录
	function clear(){
		$("p").html("");
		$("span").html("");
		//总能耗  
		totalEnerOil="",totalEnerGas="",totalEnerEle="",totalEnerOilPer="",totalEnerGasPer="",totalEnerElePer="",
		//百公里能耗
		kmEnerOil="",kmEnerGas="",kmEnerEle="",kmEnerOilPer="",kmEnerGasPer="",kmEnerElePer="",
		//能耗排名
		enerRankOil="",enerRankEle="",enerRankGas="",enerRankOilPer="",enerRankElePer="",enerRankGasPer="";
		
	}
	//顶部日期类型
	$(".time-select a").click(function(){
		clear();
		if(carId){
	        dateType = $(this).attr('val');
	        //标题时间
			titleStr = chartTitle(dateType);
	        $(this).addClass("active");
	        $(this).siblings().removeClass("active");
	        //燃料类型、总能耗、百公里能耗、能耗排名
			getCarAnaEnerIdx(carId,dateType);
			//车辆百公里能耗趋势     车辆里程趋势
			getCarAnaEnerTrend(carId,dateType);
			//能耗排名
			getCarAnaEnerRank(carId,dateType);
			//能耗转速分析
			getCarAnaEnergySpdAna(carId,dateType);
		}
    });
	
	//总能耗
	$(".tab-panel3 a").click(function(){
		var trendCarTmp=[],trendLineTmp=[];
		enerType = $(this).attr('val');
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
        if(enerType=="oil"){
        	checkType(totalEnerOil,"totalEnerTmp","<em>L</em>");
        	checkType(kmEnerOil,"hundKmEnerTmp","<em>L</em>");
        	checkType(enerRankOil,"enerRank","");
        	clickPanel(totalEnerOilPer!=""?totalEnerOilPer:"",kmEnerOilPer!=""?kmEnerOilPer:"",enerRankOilPer!=""?enerRankOilPer:""); 
        	//油 曲线图
        	trendCarTmp=carOilData;
        	trendLineTmp=lineOilData;
        	if(trendCarTmp=="" && trendLineTmp ==""){
        		hundKmEnerCharts("暂无数据",tmpDate,'单车百公里电耗趋势',trendCarTmp,'线路平均百公里电耗趋势',trendLineTmp);
            	//排名
            	writeTrHtml("");
            	//饼图
            	spdAna("暂无数据","");
        	}else{
        		hundKmEnerCharts(titleStr,tmpDate,'单车百公里油耗趋势',trendCarTmp,'线路平均百公里油耗趋势',trendLineTmp);
            	//排名
            	writeTrHtml(trHtmlOil);
            	//饼图
            	spdAna(titleStr,energyPctOil);
        	}
        }else if(enerType=="ele"){
        	checkType(totalEnerEle,"totalEnerTmp","<em>KW/H</em>");
        	checkType(kmEnerEle,"hundKmEnerTmp","<em>KW/H</em>");
        	checkType(enerRankEle,"enerRank","");
        	clickPanel(totalEnerElePer!=""?totalEnerElePer:"",kmEnerElePer!=""?kmEnerElePer:"",enerRankElePer!=""?enerRankElePer:""); 
        	//电 折线图
        	trendCarTmp=carEleData;
        	trendLineTmp=lineEleData;
        	if(trendCarTmp=="" && trendLineTmp ==""){
        		hundKmEnerCharts("暂无数据",tmpDate,'单车百公里电耗趋势',trendCarTmp,'线路平均百公里电耗趋势',trendLineTmp);
            	//排名
            	writeTrHtml("");
            	//饼图
            	spdAna("暂无数据","");
        	}else{
        		hundKmEnerCharts(titleStr,tmpDate,'单车百公里电耗趋势',trendCarTmp,'线路平均百公里电耗趋势',trendLineTmp);
            	//排名
            	writeTrHtml(trHtmlEle);
            	//饼图
            	spdAna(titleStr,energyPctEle);
        	}
        }else if(enerType=="gas"){
        	checkType(totalEnerGas,"totalEnerTmp","<em>L</em>");
        	checkType(kmEnerGas,"hundKmEnerTmp","<em>L</em>");
        	checkType(enerRankGas,"enerRank","");
        	clickPanel(totalEnerGasPer!=""?totalEnerGasPer:"",kmEnerGasPer!=""?kmEnerGasPer:"",enerRankGasPer!=""?enerRankGasPer:""); 
        	//气 曲线图
        	trendCarTmp=carGasData;
        	trendLineTmp=lineGasData;
        	if(trendCarTmp=="" && trendLineTmp ==""){
        		hundKmEnerCharts("暂无数据",tmpDate,'单车百公里气耗趋势',trendCarTmp,'线路平均百公里气耗趋势',trendLineTmp);
            	//排名
            	writeTrHtml("");
            	//饼图
            	spdAna("暂无数据","");
        	}else{
        		hundKmEnerCharts(titleStr,tmpDate,'单车百公里气耗趋势',trendCarTmp,'线路平均百公里气耗趋势',trendLineTmp);
            	//排名
            	writeTrHtml(trHtmlGas);
            	//饼图
            	spdAna(titleStr,energyPctGas);
        	}
        }
    });
	
	//燃料类型、总能耗、百公里能耗、能耗排名
    function getCarAnaEnerIdx(carId,dateType){
    	$("#totalEnerOilPer").removeClass();
    	$("#totalEnerHundKmPer").removeClass();
    	$("#totalEnerRankPer").removeClass();
    	$.ajaxExt({
			url :"/carAna/getCarAnaEnerIdx.htm",
			data : {'carId' : carId,'dateType' : dateType}
		}).done(function(data){
			if(data.length==1){
				$("#fuelType").html(data[0].idxValue);
			}else if(data.length>1){
				$(data).each(function(i,item) {
					if(item.idxType=="040501"){
						$("#fuelType").html(item.idxValue);
					}else if(item.idxType=="040502"){
						totalEnerOil=item.idxValue;
						totalEnerOilPer=item.idxMomPer;
						if(totalEnerOilPer.replace("%","")>-1){
							$("#totalEnerOilPer").addClass("icon-arrow-up"); 
						}else{
							$("#totalEnerOilPer").addClass("icon-arrow-up");
							$("#totalEnerOilPer").addClass("icon-arrow-down");
						}
						$("#totalEnerTmp").html(totalEnerOil +"L");
						$("#totalEnerOilPer").html(totalEnerOilPer);
					}else if(item.idxType=="040503"){
						totalEnerEle=item.idxValue;
						totalEnerElePer=item.idxMomPer;
					}else if(item.idxType=="040504"){
						totalEnerGas=item.idxValue;
						totalEnerGasPer=item.idxMomPer;
					}else if(item.idxType=="040505"){
						kmEnerOil=item.idxValue;
						kmEnerOilPer=item.idxMomPer;
						if(kmEnerOilPer.replace("%","")>-1){
							$("#totalEnerHundKmPer").addClass("icon-arrow-up"); 
						}else{
							$("#totalEnerHundKmPer").addClass("icon-arrow-up");
							$("#totalEnerHundKmPer").addClass("icon-arrow-down");
						}
						$("#hundKmEnerTmp").html(kmEnerOil +"L");
						$("#totalEnerHundKmPer").html(kmEnerOilPer);
					}else if(item.idxType=="040506"){
						kmEnerEle=item.idxValue;
						kmEnerElePer=item.idxMomPer;
					}else if(item.idxType=="040507"){
						kmEnerGas=item.idxValue;
						kmEnerGasPer=item.idxMomPer;
					}else if(item.idxType=="040508"){
						enerRankOil=item.idxValue;
						enerRankOilPer=item.idxMomPer;
						if(enerRankOilPer.replace("%","")>-1){
							$("#totalEnerRankPer").addClass("icon-arrow-up"); 
						}else{
							$("#totalEnerRankPer").addClass("icon-arrow-up");
							$("#totalEnerRankPer").addClass("icon-arrow-down");
						}
						$("#enerRank").html(item.idxValue);
						$("#totalEnerRankPer").html(enerRankOilPer);
					}else if(item.idxType=="040509"){
						enerRankEle=item.idxValue;
						enerRankElePer=item.idxMomPer;
					}else if(item.idxType=="040510"){
						enerRankGas=item.idxValue;
						enerRankGasPer=item.idxMomPer;
					}
				});
			}else{
				$("#fuelType").html("暂无数据");
			}
		});
    }
    
    //车辆百公里能耗趋势     车辆里程趋势
    function getCarAnaEnerTrend(carId,dateType){
    	tmpDate=[],carOilData=[],lineOilData=[],carEleData=[],lineEleData=[],carGasData=[],lineGasData=[],carMileData=[],lineAvgData=[];
		$.ajaxExt({
			url :"/carAna/getCarAnaEnerTrend.htm",
			data : {'carId' : carId,'dateType' : dateType}
		}).done(function(data){
			if(data.length>0){
			$(data).each(function(i,item) {
				xAxisDateFormat(dateType,tmpDate,item.REPORT_DATE);
				if(item.IDX_TYPE=="040601"){
					if(item.TREND_VAL1=="单车百公里油耗趋势"){
						carOilData.push(parseFloat(item.TREND_VAL2)||0);
					}else if(item.TREND_VAL1=="单车百公里电耗趋势"){
						carEleData.push(parseFloat(item.TREND_VAL2)||0);
					}else if(item.TREND_VAL1=="单车百公里气耗趋势"){
						carGasData.push(parseFloat(item.TREND_VAL2)||0);
					}
				}else if(item.IDX_TYPE=="040602"){
					if(item.TREND_VAL1=="线路平均百公里油耗趋势"){
						lineOilData.push(parseFloat(item.TREND_VAL2)||0);
					}else if(item.TREND_VAL1=="线路平均百公里气耗趋势"){
						lineGasData.push(parseFloat(item.TREND_VAL2)||0);
					}else if(item.TREND_VAL1=="线路平均百公里电耗趋势"){
						lineEleData.push(parseFloat(item.TREND_VAL2)||0);
					}
				}else if(item.IDX_TYPE=="040701"){
					if(item.TREND_VAL1=="线路平均里程趋势"){
						lineAvgData.push(parseFloat(item.TREND_VAL2)||0);
					}else if(item.TREND_VAL1=="单车里程趋势"){
						carMileData.push(parseFloat(item.TREND_VAL2)||0);
					}
				}
			});
			//曲线图
			hundKmEnerCharts(titleStr,$.unique(tmpDate),'单车百公里油耗趋势',carOilData,'线路平均百公里油耗趋势',lineOilData);
			carMileTrend(titleStr,carMileData,lineAvgData);
			}else{
				hundKmEnerCharts("暂无数据","",'单车百公里油耗趋势',"",'线路平均百公里油耗趋势',"");
				carMileTrend("暂无数据","","");
			}
		});
    }
    
    //柱状图
    function carMileTrend(titleStr,carMileData,lineAvgData){
    	var tickInter;
		if(dateType=="04" || dateType=="05"){
			tickInter=2;
		}else{
			tickInter=1;
		}
		$('#carMileTrend').highcharts({
	        chart: {
	            type: 'column'
	        },
	        title: {
	            text: "车辆："+carName +' '+titleStr,
	            style:
	            	{
	            	  fontFamily: 'sans-serif',
            		  fontSize: '14px'
	            	}
	        },
	        subtitle: {
	            text: ''
	        },
	        credits: {
		          enabled:false
		    },
	        xAxis: {
	        	tickInterval: tickInter,
	            categories:  tmpDate
	        },
	        yAxis: {
	            min: 0,
	            title: {
	                text: 'KM'
	            }
	        },
	        tooltip: {
	            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
	            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
	                '<td style="padding:0"><b>{point.y:.1f} Km</b></td></tr>',
	            footerFormat: '</table>',
	            shared: true,
	            useHTML: true
	        },
	        plotOptions: {
	            column: {
	                pointPadding: 0.2,
	                borderWidth: 0
	            }
	        },
	        series: [{
	            name: '车辆里程',
	            data:  carMileData

	        }, {
	            name: '线路平均里程',
	            data:  lineAvgData

	        }]
	    });
    }
    //能耗排名
    function getCarAnaEnerRank(carId,dateType){
		$.ajaxExt({
			url :"/carAna/getCarAnaEnerRank.htm",
			data : {'carId' : carId,'dateType' : dateType}
		}).done(function(data){
			if(data.length>0){
			//排名
			trHtmlOil="",trHtmlEle="",trHtmlGas="",trclass="",numOil=0,numEle=0,numGas=0;
			$(data).each(function(i,item) {
				if(carName ==item.LIC_PLT_NO){
					trclass="<tr class='active'>";
				}else{
					trclass="<tr>";
				}
				if(item.IDX_TYPE=="040801"){	
					numOil++;
					if(numOil<=5){
						//能耗排名 填充数据 油
						trHtmlOil+=trclass+"<td><span class='rank-tip'>"+ item.RANK_SN +"</span></td><td>"+ item.LIC_PLT_NO +"</td><td>"+ item.RANK_VAL1 +"L</td></tr>";
					}
				}else if(item.IDX_TYPE=="040802"){
					numEle++;
					if(numEle<=5){
						//能耗排名 填充数据  电
						trHtmlEle+=trclass+"<td><span class='rank-tip'>"+ item.RANK_SN +"</span></td><td>"+ item.LIC_PLT_NO +"</td><td>"+ item.RANK_VAL1 +"KW/H</td></tr>";
					}
				}else if(item.IDX_TYPE=="040803"){
					numGas++;
					if(numGas<=5){
					//能耗排名 填充数据  气
					trHtmlGas+=trclass+"<td><span class='rank-tip'>"+ item.RANK_SN +"</span></td><td>"+ item.LIC_PLT_NO +"</td><td>"+ item.RANK_VAL1 +"L</td></tr>";
					}
				}
			});
			writeTrHtml(trHtmlOil);
			}else{
				writeTrHtml("");
			}
		});
    }
    
    //饼图
    function getCarAnaEnergySpdAna(carId,dateType){
    	//饼图
		energyPctOil=[],energyPctEle=[],energyPctGas=[];
    	$.ajaxExt({
			url :"/carAna/getCarAnaEnergySpdAna.htm",
			data : {'carId' : carId,'dateType' : dateType}
		}).done(function(data){
			if(data.length>0){
			$(data).each(function(i,item) {
				if(item.IDX_TYPE=="040901"){
					//饼图 填充数据  油
					energyPctOil.push([item.RANK_VAL1, parseInt(item.RANK_VAL2)||0]);
				}else if(item.IDX_TYPE=="040902"){
					//饼图 填充数据  电
					energyPctEle.push([item.RANK_VAL1, parseInt(item.RANK_VAL2)||0]);
				}else if(item.IDX_TYPE=="040903"){
					//饼图 填充数据  气
					energyPctGas.push([item.RANK_VAL1, parseInt(item.RANK_VAL2)||0]);
				}
			});
			spdAna(titleStr,energyPctOil);
			}else{
				spdAna("暂无数据","");
			}
		});
    }
    
    //曲线图
    function hundKmEnerCharts(titleStr,tmpDate,trendCarName,trendCarTmp,trendLineName,trendLineTmp){
    	var tickInter;
		if(dateType=="04" || dateType=="05"){
			tickInter=2;
		}else{
			tickInter=1;
		}
    	$('#carHundKmEnerTrend').highcharts({
	        chart: {
	            type: 'spline'
	        },
	        title: {
	            text:"车辆："+carName +' '+titleStr,
	            style:
            	{
            	  fontFamily: 'sans-serif',
        		  fontSize: '14px'
            	}
	        },
	        subtitle: {
	            text: ''
	        },
	        xAxis: {
	        	tickInterval: tickInter,
	            categories: tmpDate
	        },
	        yAxis: {
	            title: {
	                text: '升(L)'
	            },
	        },
	        credits: {
		          enabled:false
		    },
		    tooltip: {	 
	        	crosshairs: true,
	            shared: true
	        },
	        plotOptions: {
	            line: {
	                dataLabels: {
	                    enabled: true
	                },
	                enableMouseTracking: false
	            }
	        },
	        series: [{
	            name: trendCarName,
	            data: trendCarTmp
	        }, {
	            name: trendLineName,
	            data: trendLineTmp
	        }]
	    });
    }
    
    //转速分析
    function spdAna(titleStr,energyPct){
    	$("#speedAnalysis").highcharts({
            chart: {
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            title: {
            	text: "车辆："+carName +' '+titleStr,
        	    style:
             	{
             	  fontFamily: 'sans-serif',
         		  fontSize: '14px'
             	}
            },
            tooltip: {
        	    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                    }
                }
            },
            credits: {
				enabled: false
			},
            series: [{
            	type: 'pie',
				name: '转速分析',
				data: energyPct
            }]
        });
    }
    
    //排名
    function writeTrHtml(trHtmlFlag){
    	if(trHtmlFlag==""){
    		$("#trHtml").html("<tr><td><span>车辆："+carName+" 暂无数据</span></td></tr>");
    	}else{
    		trHtml="<tr><th width='15%'>排名</th><th width='40%'>车牌号</th><th>百公里能耗</th></tr>";
        	trHtml+=trHtmlFlag;
        	$("#trHtml").html(trHtml);
    	}
    }
    //清空定义变量
    function  empVar(){
        titleStr="",
    	//所有的  ：oil-油、ele-电、gas-气、per-指标环比
    	//总能耗  
    	totalEnerOil="",totalEnerGas="",totalEnerEle="",totalEnerOilPer="",totalEnerGasPer="",totalEnerElePer="",
    	//百公里能耗
    	kmEnerOil="",kmEnerGas="",kmEnerEle="",kmEnerOilPer="",kmEnerGasPer="",kmEnerElePer="",
    	//能耗排名
    	enerRankOil="",enerRankEle="",enerRankGas="",enerRankOilPer="",enerRankElePer="",enerRankGasPer="";
    }
    //三目运算
	function checkType(tmp1,tmp2,tmp3){
		return tmp1!=""?$("#"+tmp2+"").html(tmp1 +""+tmp3+""):$("#"+tmp2+"").html("-");
	}
	//切换up down样式
	function clickPanel(temp1,temp2,temp3){
		var t1=temp1,t2=temp2,t3=temp3;
		panelFun(temp1,t1,'totalEnerOilPer');
		panelFun(temp2,t2,'totalEnerHundKmPer')
		panelFun(temp3,t3,'totalEnerRankPer')
	}
	function panelFun(temp,t,id){
		if(temp!="" ){
		if(t.replace("%","")>0){
			$("#"+id+"").addClass("icon-arrow-up"); 
		}else{
			$("#"+id+"").addClass("icon-arrow-up"); 
			$("#"+id+"").addClass("icon-arrow-down"); 
		}
			$("#"+id+"").html(temp);
		}else{
			$("#"+id+"").removeClass("icon-arrow-up");
			$("#"+id+"").html("");
		}
	}
	
    //如果左侧树有选择车辆则直接查询该车辆数据
    business.execFnByLeftTreeSelectNode('CAR', carChange);
});